import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:top_foundation/utils/theme/button.dart';
import 'package:top_foundation/utils/theme/theme.dart';
import 'package:whale/helper/route/route_navigator.dart';

import '../utils/util.dart';

class AddFilesView extends StatefulWidget {
  const AddFilesView({super.key});

  @override
  State<AddFilesView> createState() => _AddFilesViewState();
}

class _AddFilesViewState extends State<AddFilesView> {
  final List<PlatformFile> list = [];
  void _pick() async {
    final results = await FilePicker.platform.pickFiles();
    if (results == null) return;
    list.addAll(results.files);
    setState(() {});
  }

  void _preview(int index) {
    final file = list[index];
    Get.toWeb(
      url: file.path!,
      title: file.name,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        IButton.text(
            onPressed: _pick,
            text: Ls.uploadfile,
            textStyle: TextStyle(
              color: ITheme.selectedColor,
              fontSize: 15.sp,
            )),
        ...List.generate(list.length, (index) {
          final m = list[index];
          return Container(
            height: 40.r,
            child: Row(
              children: [
                Expanded(
                  child: IButton.text(
                      alignment: Alignment.centerLeft,
                      onPressed: () {
                        _preview(index);
                      },
                      text: Uri.file(m.path!).pathSegments.last,
                      textStyle: TextStyle(
                          color: ITheme.selectedColor, fontSize: 13.sp)),
                ),
                IButton.icon(
                    onPressed: () {
                      list.removeAt(index);
                      setState(() {});
                    },
                    icon: Icon(
                      Icons.delete_rounded,
                      color: Colors.red,
                      size: 25.r,
                    ))
              ],
            ),
          );
        })
      ],
    );
  }
}
